<?php
namespace app\crm\controller;
use think\Loader;

class Saleprogram extends CrmController
{
    /**
     * 商品销售方案列表界面
     * @return [type] [description]
     */
    public function saleProgramList()
    {	
		//分配增删改的权限
		$menuname = "Sale Program List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
        // 模板输出
        return $this->fetch('saleProgramList');
    }

	/**
	 * 添加销售方案界面
	 */
    public function addSaleProgram()
    {
        //分配增删改的权限
        $menuname = "Sale Program List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);

        $typeData = Loader::model('Saleprogram')->getProgramType();
        $this->assign("typeData",$typeData);
		
		// 模板输出
        return $this->fetch('addSaleProgram');
    }

	/**
	 * 添加销售方案
	 */
    public function insertSaleProgram ()
    {
        Loader::model('Saleprogram')->insertSaleProgram();
    }

	/**
	 * 更新商品界面
	 */
    public function editSaleProgram()
    {
        //分配增删改的权限
        $menuname = "Sale Program List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		
		$typeData = Loader::model('Saleprogram')->getProgramType();
        $this->assign("typeData",$typeData);
        //$where = 'id='.$_REQUEST['id'];
        $saleProgramData = Loader::model('Saleprogram')->getSaleProgramInfo();
        $this->assign("saleProgramData",$saleProgramData);

        return $this->fetch('editSaleProgram');
    }
	

	//编辑界面商品详情
    public function saleProgramGoods()
    {
        $arrData = Loader::model('Saleprogram')->getSaleProgramGoods($_REQUEST["id"]);
        echo json_encode($arrData);
    }

	/**
     * 调用model的商品updateGoods方法
     */
    public function updateSaleProgram()
    {
        Loader::model('Saleprogram')->updateSaleProgram();
    }


    /**
     * 删除销售方案
     * @return [type] [description]
     */
    public function delSaleProgram()
    {
        $id = $_REQUEST["id"];
        Loader::model('Saleprogram')->delSaleProgram($id);
    }
	
	/**
     * 获取方案列表数据
     */
    public function saleProgramDataList(){
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];

		//需先判断传值是否为空
        $where = "(1 ";
        $where .= empty($_REQUEST["program_name"])?"":" AND program_name like '%".$_REQUEST["program_name"]."%'";
		$where .= empty($_REQUEST["enable"])?"":" AND enable = '".$_REQUEST["enable"]."'";
		if ($_REQUEST['task_id'] && empty($_REQUEST["type_id"])) {
            // 外呼任务id有值 说明是外呼开单调用的这个页面 数据只显示外呼可以看到的
            $typeData = Loader::model('Saleprogram')->getProgramType();
            $type = ' AND type_id IN (';
            foreach ($typeData as &$val) {
                $type .= $val['id'].',';
            }
            $type = rtrim($type, ',');
            $type .= ')';
            $where .= $type;
        } else {
            $where .= empty($_REQUEST["type_id"])?"":" AND type_id = '".$_REQUEST["type_id"]."'";
        }
        $where .= " )";
		
        $arrData = Loader::model('Saleprogram')->getsaleProgramData($page,$limit,$where);
        echo json_encode($arrData);
    }
	
	/**
     * 加载方案分类模板
     */
	public function saleProgramTypeList()
	{
		//分配增删改的权限
        $menuname = "SaleProgram Type List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);

        return $this->fetch('saleProgramTypeList');
	}
	
	/**
     * 获取方案FENLEI列表数据
     */
    public function saleProgramTypeDataList(){
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
        $arrData = Loader::model('Saleprogram')->saleProgramTypeDataList($page,$limit);
        echo json_encode($arrData);
    }
	
	/**
     * 调用model的方案FENLEI方法
     */
    public function insertSaleProgramType()
    {
        Loader::model('Saleprogram')->insertSaleProgramType();
    }
	
	/**
     * 删除销售方案分类
     * @return [type] [description]
     */
    public function delSaleProgramType()
    {
        $id = $_REQUEST["id"];
        Loader::model('Saleprogram')->delSaleProgramType($id);
    }
	

}